package com.tianji.learning.mq;

import cn.hutool.core.collection.CollectionUtil;
import com.tianji.common.constants.MqConstants;
import com.tianji.common.msg.LikedTimesDTO;
import com.tianji.learning.entity.InteractionReply;
import com.tianji.learning.service.InteractionReplyService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
@RequiredArgsConstructor
public class LikedRecordLisenter {

    private final InteractionReplyService replyService;

    /**
     * QA点赞系统 消费者
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "qa.liked.tubes.queue", durable = "true"),
            exchange = @Exchange(value = MqConstants.Exchange.LIKE_RECORD_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.QA_LIKED_TIMES_KEY))
    public void onMsg(List<LikedTimesDTO> list) {
        log.debug("收到点赞消息：{}", list);
        // 消息转po
        List<InteractionReply> replyList = new ArrayList<>();
        for (LikedTimesDTO dto : list) {
            InteractionReply reply = new InteractionReply();
            reply.setId(dto.getBizId());
            reply.setLikedTimes(dto.getLikedTimes());
            replyList.add(reply);
        }
        replyService.updateBatchById(replyList);
    }
//    public void onMsg(LikedTimesDTO dto) {
//        log.debug("收到点赞消息：{}", dto);
//        InteractionReply reply = replyService.getById(dto.getBizId());
//        if (reply == null) {
//            return;
//        }
//        reply.setLikedTimes(dto.getLikedTimes());
//        replyService.updateById(reply);
//    }
}
